package com.google.code.opengw.server.support;

import java.security.cert.X509Certificate;

import com.google.code.opengw.support.OpenGWException;

public class CertificateUtils {
	public static String getCertificateSubjectDNCN(X509Certificate certificate) throws OpenGWException{
		String certificateCN = certificate.getSubjectDN().getName();
		String[] subjectArr = certificateCN.split(",");
		for(String subjectVal : subjectArr){
			if(subjectVal.trim().startsWith("CN")){
				return subjectVal.substring(subjectVal.indexOf("=") + 1).trim();
			}
		}
		throw new OpenGWException(OpenGWException.CERTIFICATE_SUBJECTDNCN_NOT_FOUND);
	}
	
	public static String getCertificateIssuerDNCN(X509Certificate certificate) throws OpenGWException{
		String certificateCN = certificate.getIssuerDN().getName();
		String[] subjectArr = certificateCN.split(",");
		for(String subjectVal : subjectArr){
			if(subjectVal.trim().startsWith("CN")){
				return subjectVal.substring(subjectVal.indexOf("=") + 1).trim();
			}
		}
		throw new OpenGWException(OpenGWException.CERTIFICATE_ISSUERDNCN_NOT_FOUND);
	}
}
